package com.qfedu.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.params.UserDetailUpdateParam;
import com.qfedu.provider.entity.DbUserDetail;
import com.qfedu.provider.entity.DbUserdetailLog;
import com.qfedu.provider.mapper.DbUserDetailMapper;
import com.qfedu.provider.mapper.DbUserdetailLogMapper;
import com.qfedu.provider.service.DbUserDetailService;
import com.qfedu.utils.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Administrator
 * @description 针对表【db_user_detail(用户详情，个人资料)】的数据库操作Service实现
 * @createDate 2024-11-23 11:16:39
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DbUserDetailServiceImpl extends ServiceImpl<DbUserDetailMapper, DbUserDetail>
        implements DbUserDetailService {

    private final DbUserDetailMapper dbUserDetailMapper;
    private final DbUserdetailLogMapper dbUserdetailLogMapper;

    @Override
    public R findById(Integer userId, String token) {
        // 1. 验证token
        if (token == null) {
            return R.error("token为空.没有登陆");
        }
        // 2. 根据userId查询用户详情
        LambdaQueryWrapper<DbUserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbUserDetail::getUserId, userId);
        wrapper.eq(DbUserDetail::getDeleted, 1);
        DbUserDetail userDetail = dbUserDetailMapper.selectOne(wrapper);
        if (userDetail == null) {
            return R.error("用户详情不存在");
        }
        return R.ok(userDetail);

    }

    @Override
    public R lgDelUser(Integer userId,String ip) {
        // 1. 根据userId查询用户详情
        if(userId==null){
            return R.error("用户id为空");
        }
        LambdaQueryWrapper<DbUserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbUserDetail::getUserId, userId);
        wrapper.eq(DbUserDetail::getDeleted, 1);
        DbUserDetail dbUserDetail = dbUserDetailMapper.selectOne(wrapper);
        if (dbUserDetail == null) {
            return R.error("用户详情不存在");
        }
        // 2. 逻辑删除用户详情
        dbUserDetail.setDeleted(0);
        dbUserDetailMapper.updateById(dbUserDetail);
        DbUserdetailLog userdetailLog = new DbUserdetailLog(dbUserDetail.getUserId(), ip, "逻辑删除了用户的详细信息");
        dbUserdetailLogMapper.insert(userdetailLog);
        return R.ok("删除成功");
    }

    @Override
    public R updateUserDetail(UserDetailUpdateParam updateParam,String ip) {
        // 1. 根据userId查询用户详情是否存在
        LambdaQueryWrapper<DbUserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbUserDetail::getUserId, updateParam.getUserId());
        DbUserDetail dbUserDetail = dbUserDetailMapper.selectOne(wrapper);
        if (dbUserDetail == null) {
            return R.error("用户详情不存在");
        }
        BeanUtils.copyProperties(updateParam, dbUserDetail);
        dbUserDetail.setUpdateTime(new Date());
        dbUserDetailMapper.insertOrUpdate(dbUserDetail);
        // 2. 记录在日志中
        DbUserdetailLog userdetailLog = new DbUserdetailLog(dbUserDetail.getUserId(), ip, "修改用户详情信息操作");
        dbUserdetailLogMapper.insert(userdetailLog);
        return R.ok(null);
    }
}




